#include "StringManager.h"


string StringManager::splitByValue(string input, string * rest, char value){
       int pos = input.find(value);
       if (pos < 0){
          pos = input.length();
          *rest = "";
       }
       else
           *rest = input.substr(pos+1);
       return input.substr(0,pos);
}
             
double StringManager::string2num(string input){
       return atof(input.c_str());
}
             
string StringManager::num2string(double input){
       stringstream* ss = new stringstream(ios_base::out);
       *ss << input;
       string str = ss->str();
       delete ss;
       return str;
}

string StringManager::int2string(int input){
       stringstream* ss = new stringstream(ios_base::out);
       *ss << input;
       string str = ss->str();
       delete ss;
       return str;
}

string StringManager::toLower(string input){

	string output="";
	for(unsigned int i=0; i<input.length();i++){
		output+=tolower(input[i]);
	};

	return output;
}

string StringManager::getTerm(string input, string * rest){
       vector<string> separatorList;
       vector<string> puncMarks;


       separatorList.clear();
       puncMarks.clear();
       
       separatorList.push_back("\t");//9
       separatorList.push_back("\n");//10
       separatorList.push_back("\r");//13
       separatorList.push_back(" ");//32



       puncMarks.push_back("!");//33
       puncMarks.push_back("\"");//34
       puncMarks.push_back("#");//35
       puncMarks.push_back("\'");//39
       puncMarks.push_back("(");//40
       puncMarks.push_back(")");//41
       puncMarks.push_back("*");//42
       puncMarks.push_back("+");//43
       puncMarks.push_back(","); //44
       puncMarks.push_back("-");//45
       puncMarks.push_back(".");  //46
       puncMarks.push_back("/");  //47
       puncMarks.push_back(":");//58
       puncMarks.push_back(";");//59
       puncMarks.push_back("?");//63
       puncMarks.push_back("[");//91
       puncMarks.push_back("\\"); //92
       puncMarks.push_back("]");//93
       puncMarks.push_back("^");//94
       puncMarks.push_back("_");//95
       puncMarks.push_back("{");//123
       puncMarks.push_back("|");//124
       puncMarks.push_back("}");//125
       puncMarks.push_back("~");//126
       puncMarks.push_back("¬");//170
       //puncMarks.push_back("‘");//8216
       //puncMarks.push_back("“");//8220




       /*bool loop = true;
       while (loop){
             loop = false;
             for (unsigned int i = 0; i < puncMarks.size(); i++){
                 int auxpos = input.find(puncMarks.at(i));
                 if (0 == auxpos){
                    input = input.substr(1);
                    loop = true;
                 }
             }
       }*/

	   bool loop = true;
       while (loop){
    	   if(input.length()>0)
    	   {
			   string buscar=input.substr(0,1);
			   if (std::binary_search(puncMarks.begin(), puncMarks.end(), buscar))
			   {
				   input = input.substr(1);
			   }
			   else
			   {loop = false;}
    	   }
    	   else
    	   {loop = false;}
       }
       


       int pos = MAX;
       for (unsigned int i = 0; i < separatorList.size(); i++){
           int auxpos = input.find(separatorList.at(i));
           if ((auxpos >= 0)&&(pos > auxpos))
              pos=auxpos;
       }
       
       if (MAX == pos){
          pos = input.length();
          *rest = "";
       }
       else
           *rest = input.substr(pos+1);
           
       string term = input.substr(0,pos);
       
       /*loop = true;
       while (loop){
             loop = false;
             for (unsigned int i = 0; i < puncMarks.size(); i++){
                 unsigned int auxpos = term.rfind(puncMarks.at(i));
                 if (term.length()-1 == auxpos){
                    term = term.substr(0,term.length()-1);
                    loop = true;
                 }
             }
       }
*/

	   loop = true;
       while (loop){

    	   if(term.length()>0)
    	   {
			   string buscar=term.substr(term.length()-1,1);
			   if (std::binary_search(puncMarks.begin(), puncMarks.end(), buscar))
			   {
				   term = term.substr(0,term.length()-1);
			   }
			   else
			   {loop = false;}
    	   }else
		   {loop = false;}
       }

       
       return term;
}
